package com.yanghui.service.impl;

import com.yanghui.dao.VisitMapper;
import com.yanghui.pojo.Visit;
import com.yanghui.pojo.VisitExample;
import com.yanghui.service.VisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class VisitServiceImpl implements VisitService {
    @Autowired
    private VisitMapper visitMapper;

    /**
     * 查询当天是否已经访问过了
     */
    @Override
    public boolean findVisitTimes(Visit visit) {
        Visit findVisit = visitMapper.findVisitTimes(visit);
        if (findVisit != null) {
            /** 更改访问时间 */
            findVisit.setTime(new Date());
            visitMapper.updateByPrimaryKeySelective(findVisit);
            return true;
        }
        return false;
    }

    @Override
    public int addVisit(Visit visit) {
        return visitMapper.insertSelective(visit);
    }

    /**
     * 按照指定日期查询访客记录
     */
    @Override
    public List<?> selectVisitListByDate(Map<String, Object> map) {
        return visitMapper.selectVisitListByDate(map);
    }

    /**
     * 按照ID删除访问记录
     */
    @Override
    public int deleteVisitByID(Integer id) {
        return visitMapper.deleteByPrimaryKey(id);
    }



    /**
     * 按照IP分组
     */
    @Override
    public List<?> selectLikeVisitListGroupByIp(Map<String, Object> map) {
        return visitMapper.selectLikeVisitListGroupByIp(map);
    }

    /**
     * 按照指定的条件查询访问者数据
     */
    @Override
    public List<Visit> selectLikeVisitListByPage(Map map) {
        VisitExample visitExample = new VisitExample();
        VisitExample.Criteria criteria = visitExample.createCriteria();

        /** 将查询条件添加到 SQL 语句中 */
        if (map.get("ip") != null) {
            criteria.andIpEqualTo((String) map.get("ip"));
        }
        if (map.get("city") != null) {
            criteria.andCityLike("%" + (String) map.get("city") + "%");
        }
        if (map.get("platformType") != null) {
            criteria.andPlatformTypeLike("%" + (String) map.get("platformType") + "%");
        }
        if (map.get("browserType") != null) {
            criteria.andBrowserTypeLike("%" + (String) map.get("browserType") + "%");
        }
        try {
            if (map.get("startTime") != null && map.get("endTime") != null) {
                String start = (String) map.get("startTime");
                Date startTime = new SimpleDateFormat("yyyy-MM-dd").parse(start);
                String end = (String) map.get("endTime");
                Date endTime = new SimpleDateFormat("yyyy-MM-dd").parse(end);

                criteria.andTimeBetween(startTime, endTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return visitMapper.selectLikeVisitListByPage(visitExample);
    }

    /**
     * 按照IP分组查询 用户数量以及查询总记录数
     */
    @Override
    public List<?> selectVisitListByIp() {
        return visitMapper.selectVisitListByIp();
    }
}
